<!--
 * @Author: atao
 * @Date: 2023-10-24 10:06:50
 * @LastEditTime: 2023-10-27 19:44:35
 * @FilePath: \三阶段项目\pili-pili-app\src\pages\shop\index.vue
-->
<template>
  <div class="sh-box">
    <header>
      <h4>会员购</h4>
      <div class="top">
        <van-search
          class="search"
          shape="round"
          v-model="value"
          placeholder="镇魂街三部曲"
        />
        <span class="iconfont icon-leimupinleifenleileibie" @click="totypes()"></span>
        <span class="iconfont icon-gouwuche" @click="tocart()"></span>
        <span class="iconfont icon-wode" @click="tomine()"></span>
      </div>
    </header>
    <div class="types">
      <div class="item" v-for="it in types" :key="it.txt">
        <img :src="baseURL + it.path" alt="" />
        <span>{{ it.txt }}</span>
      </div>
    </div>
    <div class="goods">
      <div class="item" v-for="it in goods" :key="it.id">
        <img :src="baseURL + it.path" alt="" />

        <div class="title">{{ it.title }}</div>
        <div class="tags">
          <span class="tag" v-for="tag in it.tag" :key="tag">
            <span>{{ tag }}</span>
          </span>
        </div>
        <div class="price"><span>到手价</span>￥{{ it.price }}<span class="cart"> <van-icon name="cart-o"
                                    @click="addCartList(it)" /></span></div>
      </div>
    </div>
  </div>
</template>
<style lang="less" scoped>
@import "../../style/common.less";

// .element::-webkit-scrollbar {
//   width: 0 !important;
// }
.sh-box {
  position: relative;
  height: 100%;
  // .rel();
  // overflow-x:hidden;
  h4 {
    position: relative;
    text-align: center;
    line-height: 50px;
    &::before {
      font-family: "iconfont" !important;
      content: "\e6ab";
      color: @b7;
      position: absolute;
      left: 155px;
      top: -8px;
      transform: rotate(321deg);
    }
  }
  header {
    position: relative;
    .top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin: 0 10px;
      .van-search {
      }
    }
  }

  > .types {
    display: flex;
    overflow-x: scroll;
    //使滚动条消失但能滚动
    &::-webkit-scrollbar {
      display: none; /* Chrome, Safari, and Opera */
    }
    > .item {
      padding: 10px;
      flex: 0 0 25%;
      display: flex;
      align-items: center;
      flex-direction: column;
      text-align: center;
      > img {
        width: 50px;
        height: 50px;
      }
      > span {
        display: block;
        font-size: 16px;
        color: @g09;
      }
    }
  }
  > .goods {
 
    height: calc(100% - 105px);
    overflow-x: hidden;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;

    > .item {
     
      margin-bottom: 10px;
      border-radius: 10px;
      box-shadow: 5px;
      background: @g01;
      flex: 0 0 calc(50% - 10px);
      display: flex;
      flex-direction: column;
      overflow: hidden;
      justify-content: space-between;
      > img {
        display: block;
        width: 100%;
        border-radius: 10px;
      }
      > .title {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        font-size: 18px;
        padding: 8px;
        color: @g09;
      }
      > .tags {
         padding:2px 8px;
        display: flex;
        font-size: 13px;
        > .tag {
          margin: 0 2px;
        }
        > span {
          border-radius: 5px;
          display: block;
          padding: 1px;
          background-color: @b3;
          color: @b7;
        }
        > span:nth-child(2) {
          background-color: @p3;
          color: @p7;
        }
      }
      > .price {
 color: @b7;
         padding:0 8px;
         > .cart{
          float: right;
          line-height: 32px;
         }
        > span {
          font-size: 15px;
        //  color:@g09
        }
      }
    }
  }
}
::v-deep .van-search__content--round {
  border: 1px solid @b7;
}
::v-deep .van-search {
  padding: 10px 0;
  // background: @g03;
  width: calc(100% - 140px);
}
::v-deep .van-icon-search:before {
  color: @b7;
}
::v-deep .van-field__body:after {
  font-family: "iconfont" !important;
  font-size: 18px;
  content: "\e672";
  color: @b7;
}
.iconfont {
  font-size: 25px;
  color: @b5;
  font-weight: 300;
}
.van-icon-cart-o:before{
  font-size: 20px;
  // position: absolute;
}
</style>

<script lang="ts" setup>
/** 全局变量 */
const baseURL = window.baseURL;
import { ref } from "vue";
import { shopGoodsApi, shopTypesApi } from "../../apis/shopApis";
import { useRouter } from "vue-router";
import { cartAddApi } from "../../apis/homeApi";
const types: any = ref([]);
const value = ref("");
const goods:any = ref([]);
const router=useRouter()
//获取types数据
shopTypesApi().then((_res: any) => {
  if (_res.code == 200) {
    types.value = _res.data;
  }
});
//获取goods数据
shopGoodsApi(3, 20).then((_res: any) => {
  if (_res.code == 200) {
    goods.value = _res.data.rows;
    console.log(_res.data,goods);
  }
});

/** 跳转到分类页面 */
function totypes() {
    router.push({
        path: '/types',
    })
}
/** 跳转到购物车页面 */
function tocart() {
    router.push({
        path: '/',
    })
}
/** 跳转到我的页面 */
function tomine() {
    router.push({
        path: '/mine',
    })
}
// 添加购物车l
// const 
async function addCartList(it: any) {
    console.log(it);
   const userId = localStorage.getItem('userid')
   console.log(userId)
    let _res = await cartAddApi({
        userId: userId,
        price: it.price,
        id: it.id,
        time: Date.now(),
        count: 1,
        name: it.title,
        detailaddress: '--',
        bgUrl: it.path,
    })
    if (_res.code === 200) {
        console.log(_res)
    } else {
        console.log(_res);

    }
}
</script>